package com.chujian.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chujian.dao.FoodListDao;
import com.chujian.dao.OrderDao;
import com.chujian.daoImpl.FoodListDaoImpl;
import com.chujian.daoImpl.OrderDaoImpl;
import com.chujian.jpush.Jdpush;
import com.chujian.jpush.MessagePush;
import com.chujian.model.FoodList;
import com.chujian.model.NotificationBean;
import com.chujian.model.Order;
import com.chujian.util.JsonUtil;
import com.google.gson.Gson;
/**
 *  
 * 实现订单提交的功能
 * @author 周映凯
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Order mOrder;
	private List<FoodList>mList;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
//		获取客户端发送过来的数据
		String jsondata=request.getParameter("jsonString");
		Order order=JsonUtil.OrderFromJson(jsondata);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		Date date=new Date();
		Long time=date.getTime();
//		System.currentTimeMillis();获取当前时间毫秒数
		order.setOrderId(time.toString().trim());
		order.setOrderTime(time.toString().trim());
		System.out.println(time);
		FoodListDao flDao=new FoodListDaoImpl();
		OrderDao orderDao=new OrderDaoImpl();
		boolean result=orderDao.addOrder(order);
		List<FoodList>list=order.getFoodList();
		for(FoodList fl:list){
			fl.setOrderId(order.getOrderId());
			flDao.addFoodList(fl);
		}
		if(result){
			mOrder= orderDao.qeuryByOrderId(order.getOrderId());
			mList=flDao.qeuryByOrderId(order.getOrderId());
			mOrder.setFoodList(mList);
		}
		Gson gson =new Gson();
		String jsonString =gson.toJson(mOrder);
		
		out.write(jsonString);
		System.out.println("jsonString:");
		System.out.println(jsonString);
		System.out.println(mOrder);
		out.flush();
//			通知卖家客户端
		if(result){
			String notice=gson.toJson(new NotificationBean("OrderCurrentListActivity", "您有新订单", "订单编号是："+mOrder.getOrderId()));
//			Jdpush.sendPush(notice, "您有新订单");
			Jdpush.sendPushAlias(notice, "您有新订单", "admin");
		}
		
	}
	
}
